from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render, redirect
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from www.forms.account import LoginForm, SmsLoginForm, SendSmsForm
from www import models
from django.conf import settings
from utils.encrypt import md5_string


def login(request):
    if request.method == 'GET':
        form = LoginForm()
        return render(request, 'login.html', {'form': form})
    form = LoginForm(data=request.POST)
    if not form.is_valid():
        return render(request, 'login.html', {'form': form})

    data_dict = form.cleaned_data
    role = data_dict.pop('role')
    # data_dict['password'] = md5_string(data_dict['password'])
    if role == '1':
        user_object = models.Administrator.objects.filter(active=1).filter(**data_dict).first()
    elif role == '2':
        user_object = models.Customer.objects.filter(active=1).filter(**data_dict).first()
    if not user_object:
        form.add_error("password", "用户名或密码错误")
        return render(request, 'login.html', {'form': form})
    # 用户存在，则将用户信息存储到session中
    mapping = {"1": "ADMIN", "2": "CUSTOMER"}
    request.session[settings.NB_SESSION_KEY] = {
        'id': user_object.id,
        'role': mapping[role],
        'name': user_object.username,
    }
    # print('login-----------------')
    # print(request.session.get(settings.NB_SESSION_KEY))
    # print('login-----------------')
    return redirect(settings.HOME_URL)

def logout(request):
    request.session.clear()
    return redirect('login')

def sms_login(request):
    if request.method == 'GET':
        form = SmsLoginForm()
        return render(request, 'sms_login.html', {'form': form})

    form = SmsLoginForm(data=request.POST)
    if not form.is_valid():
        return JsonResponse({'status': False, 'msg': form.errors})
    role = form.cleaned_data['role']
    mobile = form.cleaned_data['mobile']
    if role == '1':
        user_object = models.Administrator.objects.filter(active=1).filter(mobile=mobile).first()
    elif role == '2':
        user_object = models.Customer.objects.filter(active=1).filter(mobile=mobile).first()
    if not user_object:
        return JsonResponse({'status': True, 'msg': {'mobile': '手机号不存在'}})
    # 用户存在，则将用户信息存储到session中
    mapping = {"1": "ADMIN", "2": "CUSTOMER"}
    request.session[settings.NB_SESSION_KEY] = {
        'id': user_object.id,
        'role': mapping[role],
        'name': user_object.username,
    }
    print(request.session.get(settings.NB_SESSION_KEY))
    return JsonResponse({'status': True, 'msg': 'OK', 'data': settings.HOME_URL})


@csrf_exempt
def send_sms(request):
    print(request.POST)
    form = SendSmsForm(data=request.POST)
    if not form.is_valid():
        return JsonResponse({'status': False, 'msg': form.errors})
    return JsonResponse({'status': True, 'msg': 'OK'})


def home(request):
    return render(request, 'home.html')


def user(request):
    return render(request,'user.html')


def add_user(request):
    return render(request,'add_user.html')


def edit_user(request,uid):
    return HttpResponse("edit_user")


def multi_import(request):
    # return HttpResponse("multi_import")
    return render(request,'multi_import.html')
